<?php

class Libs_Database {

//    private $_host = "localhost";
//    private $_user = "root";
//    private $_password = "";
//    private $_name = "qlns";

    private $_conndb = false;
    public $_last_query = null;
    public $_affected_rows = 0;
    public $_insert_keys = array();
    public $_insert_values = array();
    public $_update_sets = array();
    public $_id;

    public function __construct() {
        $this->connect();
    }

    private function connect() {
        $this->_conndb = mysql_connect(DB_HOST, DB_USER, DB_PASS);

        if (!$this->_conndb) {
            die("Quá trình kết nối CSDL xảy ra lỗi: <br/>" . mysql_error());
        } else {
            $_select = mysql_select_db(DB_DATA, $this->_conndb);
            if (!$_select) {
                die("Lựa chọn CSDL không đúng: <br/>" . mysql_error());
            }
        }

        mysql_set_charset("utf8", $this->_conndb);
    }

//    public function __destruct() {
//        $this->close();
//    }

    public function close() {
        if (!mysql_close($this->_conndb)) {
            die("Đóng kết nối CSDL không thành công!");
        }
    }

    public function escape($value) {
        if (function_exists("mysql_real_escape_string")) {
            if (get_magic_quotes_gpc()) {
                $value = stripslashes($value);
            }
            $value = mysql_real_escape_string($value);
        } else {
            if (!get_magic_quotes_gpc()) {
                $value = addslashes($value);
            }
        }
        return $value;
    }

    public function query($sql) {
        $this->_last_query = $sql;
        $result = mysql_query($sql, $this->_conndb);
        $this->displayQuery($result);
        return $result;
    }

    public function displayQuery($result) {
        if (!$result) {
            $output = "Truy vấn CSDL sai: " . mysql_error() . "<br/>";
            $output .= "Truy vấn: " . $this->_last_query;
            die($output);
        } else {
            $this->_affected_rows = mysql_affected_rows($this->_conndb);
        }
    }

    public function fetchAll($sql) {
        $result = $this->query($sql);
        $out = array();
        while ($row = mysql_fetch_assoc($result)) {
            $out[] = $row;
        }
        mysql_free_result($result);
        return $out;
    }

    public function fetchAllO($sql) {
        $result = $this->query($sql);
        return $result;
    }

    public function fetchOne($sql) {
        $out = $this->fetchAll($sql);
        return array_shift($out);
    }

    public function fetchOneO($sql) {
        $out[] = $this->fetchAllO($sql);
        return array_shift($out);
    }

    public function lastId() {
        return mysql_insert_id($this->_conndb);
    }

    public function prepareInsert($array = null) {
        if (!empty($array)) {
            foreach ($array as $key => $value) {
                $this->_insert_keys[] = $key;
                $this->_insert_values[] = $this->escape($value);
            }
        }
    }

    public function insert($table = null) {
        if (!empty($table) && !empty($this->_insert_keys) && !empty($this->_insert_values)) {
            $sql = "INSERT INTO `{$table}` (";
            $sql .= implode(", ", $this->_insert_keys);
            $sql .= ") VALUES('";
            $sql .= implode("', '", $this->_insert_values);
            $sql .= "')";

            if ($this->query($sql)) {
                $this->_id = $this->lastId();
                return true;
            }
            return false;
        }
    }

    public function prepareUpdate($array = null) {
        if (!empty($array)) {
            foreach ($array as $key => $value) {
                $this->_update_sets[] = "`{$key}` = '" . $this->escape($value) . "'";
            }
        }
    }

    public function update($table = null, $id = null) {
        if (!empty($table) && !empty($id) && !empty($this->_update_sets)) {
            $sql = "UPDATE `{$table}` SET ";
            $sql .= implode(", ", $this->_update_sets);
            $sql .= " WHERE `id` = '" . $this->escape($id) . "'";
            return $this->query($sql);
        }
    }

    public function updatePro($table = null, $id = null) {
        if (!empty($table) && !empty($id) && !empty($this->_update_sets)) {
            $sql = "UPDATE `{$table}` SET ";
            $sql .= implode(", ", $this->_update_sets);
            $sql .= " WHERE `pro_id` = '" . $this->escape($id) . "'";
            return $this->query($sql);
        }
    }

    public function updateCat($table = null, $id = null) {
        if (!empty($table) && !empty($id) && !empty($this->_update_sets)) {
            $sql = "UPDATE `{$table}` SET ";
            $sql .= implode(", ", $this->_update_sets);
            $sql .= " WHERE `cat_id` = '" . $this->escape($id) . "'";
            return $this->query($sql);
        }
    }

    public function updateSale($table = null, $id = null) {
        if (!empty($table) && !empty($id) && !empty($this->_update_sets)) {
            $sql = "UPDATE `{$table}` SET ";
            $sql .= implode(", ", $this->_update_sets);
            $sql .= " WHERE `sale_id` = '" . $this->escape($id) . "'";
            return $this->query($sql);
        }
    }

    function getCurrentUrl() {
        $url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
        $url = $url . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        return $url;
    }

    //Get user IP
    function getUserIP() {
        $client = @$_SERVER['HTTP_CLIENT_IP'];
        $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
        $remote = $_SERVER['REMOTE_ADDR'];

        if (filter_var($client, FILTER_VALIDATE_IP)) {
            $ip = $client;
        } elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
            $ip = $forward;
        } else {
            $ip = $remote;
        }

        return $ip;
    }

    function getCurrentTime() {
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $date = date('Y-m-d h:i:s', time());
        return $date;
    }

}

?>
